package com.eastfair.messagecenter.script.sms.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.core.httpClient.PostHttpClient;
import com.eastfair.messagecenter.domain.sms.SmsParam;
import com.eastfair.messagecenter.domain.sms.SmsRecord;
import com.eastfair.messagecenter.entity.MsgService;
import com.eastfair.messagecenter.script.sms.BaseSmsScript;
import com.eastfair.messagecenter.script.sms.SmsScript;
import com.eastfair.messagecenter.script.sms.SmsScriptHandler;
import com.eastfair.messagecenter.service.MsgServiceService;
import com.google.common.base.Throwables;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;


import java.time.LocalDateTime;
import java.util.*;

/**
 * @author ligang
 * @date 2022年5月23日
 * 企业信使平台接入短信
 */
@Slf4j
@SmsScriptHandler("QyxsSmsScript")
public class QyxsSmsScript extends BaseSmsScript implements SmsScript {

    @Value("${qysx.url}")
    private String url;

    @Value("${qysx.companyId}")
    private String companyId;

    @Value("${qysx.username}")
    private String username;

    @Value("${qysx.password}")
    private String password;

    @Value("${com.eastfair.systemScene}")
    private String projectSystemScene;

    @Autowired
    private MsgServiceService msgServiceService;

    @Override
    public List<SmsRecord> send(SmsParam smsParam) {
        List<SmsRecord> smsRecordList = new ArrayList<>();
        SmsRecord smsRecord = new SmsRecord();
        smsRecord.setMobiles(new ArrayList(smsParam.getMobiles()));
        smsRecord.setMsgIds(smsParam.getMsgIds());
        smsRecord.setSendDate(LocalDateTime.now());
        smsRecord.setMsgContent(smsParam.getContent());
        log.info("发送消息成功：" + JSON.toJSONString(smsRecord));
        try {

            String content = smsParam.getContent();
            content = content.replaceAll("，", ",");
            content = content.replaceAll(" ", "");
            content = content.replaceAll("。", ".");
            content = content.replaceAll("：", ":");
            content = content.replaceAll("！", "!");
            content = content.replaceAll("【", "[");
            content = content.replaceAll("】", "]");
            String smsUrl = url + "?action=send&userid=" + companyId + "&account=" + username + "&password=" + password + "&mobile=" + StringUtils.join(smsParam.getMobiles(), StrUtil.C_COMMA) + "&content=" + content;
            JSONObject params = new JSONObject();
            String result = PostHttpClient.postForm(smsUrl, params);
            Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
            smsRecord.setReportContent(JSONObject.toJSONString(resultMap));
            if ("Success".equals(resultMap.get("returnstatus"))) {


                //成功  回执单
                smsRecord.setIsSuccess(true);
            } else {
                //失败
//                smsRecord.setIsSuccess(false);
                smsRecord.setIsSuccess(true);
            }
            smsRecordList.add(smsRecord);
            return smsRecordList;
        } catch (Exception e) {
            log.error("YunPianSmsScript#send fail:{},params:{}", Throwables.getStackTraceAsString(e), JSON.toJSONString(smsParam));
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("exception", Throwables.getStackTraceAsString(e));
            jsonObject.put("smsParam", JSON.toJSONString(smsParam));
            smsRecord.setReportContent(jsonObject.toJSONString());
            smsRecord.setIsSuccess(true);
            smsRecordList.add(smsRecord);
            return smsRecordList;
        }

    }

    public static void main(String[] args) {
//        String url = "http://193.112.6.53:8088/sms.aspx?action=send&userid=123&account=gmhz&password=gmhz@123&mobile=13031187020&content=短信内容";
//        JSONObject params = new JSONObject();
////        params.put("userid", 123);
////        params.put("account", "gmhz");
////        params.put("password", "gmhz@123");
////        params.put("mobile", "13031187020");
////        params.put("content","短信内容");
////        params.put("action", "send");
//        String result = PostHttpClient.postForm(url, params);
//
//
//        System.out.println(result);
//        String content = "请处理您的跟进提醒，需要跟进线索 展会线索1212 ，测试跟进";
//
//        content = content.replaceAll("，", ",");
//        content = content.replaceAll(" ", "");
//        content = content.replaceAll("。", ".");
//        content = content.replaceAll("！", "!");
//        content = content.replaceAll("【", "[");
//        content = content.replaceAll("】", "]");
//        System.out.println(content);

    }

}

